<?php

class AuctionsController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

  public  function    actionCreateOrderAuction(){
  
  	$ordersModel=new  Orders();
  	$imagesModel=new  Images();
  	$auctionimagesModel=new  AuctionImages();
  	$id= Yii::app()->db->createCommand('select  max(id)  as id from   auction_info')->queryAll();
    $id = $id[0]['id'];
  	$arr=AuctionInfo::model()->findByPk($id);
  
  	$arr_fee=Yii::app()->db->createCommand('select  *    from   auction_fee   where   auction_id='.$id )->queryAll();
  	foreach ($arr_fee  as   $v){
  	if($v['fee_name']=="value_price"){//商品价格
  	    $arr_fee_value['value_price']=$v['fee_value'];
  	    
  	}
  	else  if($v['fee_name']=="billing_fee"){//硬收费
		$arr_fee_value['billing_fee']=$v['fee_value'];
  	}
  	else  if($v['fee_name']=="shipping_fee"){//运费
  	    $arr_fee_value['shipping_fee']=$v['fee_value'];
  	 }
  	}
  	$model_category=AuctionCategoryRelations::model()->find('auction_id=:auction_id  and   is_delete=0',array(':auction_id'=>$id));
  	$model_product=AuctionProductRelations::model()->find('auction_id=:auction_id  and   is_delete=0',array(':auction_id'=>$id));
    $model_pool=AuctionPool::model()->find('auction_id=:auction_id  ',array(':auction_id'=>$id));
    $arr_param=Yii::app()->db->createCommand('select  *    from   auction_parame   where   auction_id='.$id )->queryAll();
//     因为这里没有全有没有的情况
    $arr_param_value['drop_ship']="";
    $arr_param_value['allow_auto_bidder']="";
    $arr_param_value['allow_buy_it_now']="";
    $arr_param_value['meta_title']="";
    $arr_param_value['meta_keyword']="";
    $arr_param_value['url']="";
    $arr_param_value['meta_description']="";
    $arr_param_value['buyitnow_date']="";
    $arr_param_value['featured_home']="";
//     整个ok，去掉
    foreach ($arr_param  as   $v){
        if($v['parame_name']=="drop_ship"){
            $arr_param_value['drop_ship']=$v['parame_value'];
        }
        else  if($v['parame_name']=="allow_auto_bidder"){
            $arr_param_value['allow_auto_bidder']=$v['parame_value'];
        }
        else  if($v['parame_name']=="allow_buy_it_now"){
            $arr_param_value['allow_buy_it_now']=$v['parame_value'];
        }
        else  if($v['parame_name']=="meta_title"){
            $arr_param_value['meta_title']=$v['parame_value'];
        }
        else  if($v['parame_name']=="meta_keywords"){
            $arr_param_value['meta_keyword']=$v['parame_value'];
        }
        else  if($v['parame_name']=="url"){
               $arr_param_value['url']=$v['parame_value'];
        }
        else  if($v['parame_name']=="meta_description"){
            $arr_param_value['meta_description']=$v['parame_value'];
        }
        else  if($v['parame_name']=="buyitnow_date"){
            $arr_param_value['buyitnow_date']=$v['parame_value'];
        }       
        else  if($v['parame_name']=="featured_home"){
           $arr_param_value['featured_home']=$v['parame_value'];
        } 
        else  if($v['parame_name']=="vendor"){
            $arr_fee_value['vendor']=$v['parame_value'];
        }
    }     
       	$model_auctionbidlog=AuctionBidLog::model()->find('auction_id=:auction_id  ',array(':auction_id'=>$id));
       	$auctionsModel=Auctions::model()->findByPK($id);  
//        	if(count($auctionsModel)==0){
       		$auctionsModel=new  Auctions();
       		$auctionsModel->auction_save($arr,$arr_fee_value,$model_product,$model_category,$model_pool,$arr_param_value,$model_auctionbidlog);
//        	}
    $ordersModel->order_save($arr,$arr_fee_value,$model_auctionbidlog,$model_pool);
    $model_auctionresource=AuctionResource::model()->findAll('auction_id=:auction_id  ',array(':auction_id'=>$id));
    foreach ($model_auctionresource as   $m){
        $imagesModel->isNewRecord = true;
        $auctionimagesModel->isNewRecord = true;
    	$imagesModel->path= $m->images->path;
    	$imagesModel->created= time();
    	if($m->images->is_delete==0){
    	    	$enable=1;
    	}else{
    	       $enable=0;
    	}
    	$imagesModel->save()&& $imagesModel->id=0;
    	$auctionimagesModel->image_id=Yii::app()->db->getLastInsertID();
    	$auctionimagesModel->auction_id=$id;
    	$auctionimagesModel->order=1;
    	$auctionimagesModel->enable=$enable;
    	$auctionimagesModel->save()&& $auctionimagesModel->id=0;
    }
  	$this->render('index',array('model'=>$auctionsModel));
  }
  public  function    actionBuyItNow(){
  	$id=$_GET['id'];
  	$auctionsModel=Auctions::model()->findByPK($id);
  	if(count($auctionsModel)==0){
  		$auctionsModel=new  Auctions();
  		$arr=AuctionInfo::model()->findByPk($id);
  		$arr_fee=Yii::app()->db->createCommand('select  *    from   auction_fee   where   auction_id='.$id )->queryAll();
  		foreach ($arr_fee  as   $v){
  			if($v['fee_name']=="value_price"){//商品价格
  				$arr_fee_value['value_price']=$v['fee_value'];
  		
  			}
  			else  if($v['fee_name']=="billing_fee"){//硬收费
  				$arr_fee_value['billing_fee']=$v['fee_value'];
  			}
  				else  if($v['fee_name']=="shipping_fee"){//运费
  					$arr_fee_value['shipping_fee']=$v['fee_value'];
  				}
  		}
  		$model_category=AuctionCategoryRelations::model()->find('auction_id=:auction_id  and   is_delete=0',array(':auction_id'=>$id));
  		$model_product=AuctionProductRelations::model()->find('auction_id=:auction_id  and   is_delete=0',array(':auction_id'=>$id));
  		$model_pool=AuctionPool::model()->find('auction_id=:auction_id  ',array(':auction_id'=>$id));
  		$arr_param=Yii::app()->db->createCommand('select  *    from   auction_parame   where   auction_id='.$id )->queryAll();
  		//     因为这里没有全有没有的情况
  		$arr_param_value['drop_ship']="";
  		$arr_param_value['allow_auto_bidder']="";
  		$arr_param_value['allow_buy_it_now']="";
  		$arr_param_value['meta_title']="";
  		$arr_param_value['meta_keyword']="";
  		$arr_param_value['url']="";
  		$arr_param_value['meta_description']="";
  		$arr_param_value['buyitnow_date']="";
  		$arr_param_value['featured_home']="";
  		//     整个ok，去掉
  		foreach ($arr_param  as   $v){
  			if($v['parame_name']=="drop_ship"){
  				$arr_param_value['drop_ship']=$v['parame_value'];
  			}
  			else  if($v['parame_name']=="allow_auto_bidder"){
  				$arr_param_value['allow_auto_bidder']=$v['parame_value'];
  			}
			else  if($v['parame_name']=="allow_buy_it_now"){
				$arr_param_value['allow_buy_it_now']=$v['parame_value'];
			}
			else  if($v['parame_name']=="meta_title"){
				$arr_param_value['meta_title']=$v['parame_value'];
			}
			else  if($v['parame_name']=="meta_keywords"){
				$arr_param_value['meta_keyword']=$v['parame_value'];
			}
    		else  if($v['parame_name']=="url"){
    			$arr_param_value['url']=$v['parame_value'];
    		}
			else  if($v['parame_name']=="meta_description"){
				$arr_param_value['meta_description']=$v['parame_value'];
			}
			else  if($v['parame_name']=="buyitnow_date"){
				$arr_param_value['buyitnow_date']=$v['parame_value'];
			}
			else  if($v['parame_name']=="featured_home"){
				$arr_param_value['featured_home']=$v['parame_value'];
			}

		    else  if($v['parame_name']=="vendor"){
		        $arr_param_value['vendor']=$v['parame_value'];
		    }
  		}
  		$model_auctionbidlog=AuctionBidLog::model()->find('auction_id=:auction_id  ',array(':auction_id'=>$id));
  		$auctionsModel->auction_save($arr,$arr_fee_value,$model_product,$model_category,$model_pool,$arr_param_value,$model_auctionbidlog);
  		return   true;
  	}else{
  		return   true;
  	}
  	

  }
  public function actionWatchIt(){
    $customer_id = Yii::app()->request->getParam('cid',0);
    $auction_id = Yii::app()->request->getParam('aid',0);
    if($customer_id==0 || $auction_id==0){
      echo 0;
    }else{
      $favorites = Favorites::model()->find('customer_id=:customer_id   and  auction_id=:auction_id  ',array(':customer_id'=>$customer_id,':auction_id'=>$auction_id ));
      if(count($favorites) == 0){
        $favorites=new  Favorites();
        $favorites->customer_id=$customer_id;
        $favorites->auction_id=$auction_id;
        $favorites->created_time=time();
        $favorites->enable=1;
      }else{
        if($favorites->enable==1){
          $favorites->enable=0;
        }
        else if($favorites->enable==0)
          $favorites->enable=1;
      }
      $result = $favorites->save();
    
      if($result){
        echo 1;
      }
      else{
        echo 0;
      }
    }  	
  	
  }
}
